package com.atguigu.com.nio.nio_4_群聊;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/11
 */
public class GroupChatClient {
    //    定义相关的属性
    private final String HOST = "127.0.0.1";
    private final int PORT = 6667;
    private Selector selector;
    private SocketChannel socketChannel;
    private String username;

    //    构造器,完成初始化工作
    public GroupChatClient() throws IOException {
        selector = selector.open();
//        连接服务器
        socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", PORT));
//        设置非阻塞
        socketChannel.configureBlocking(false);
//        将channel注册到selector
        socketChannel.register(selector, SelectionKey.OP_READ);
//        得到username
        username = socketChannel.getLocalAddress().toString().substring(1);
        System.out.println(username + "is ok...");
    }

    //  向服务器发送消息
    public void sendInfo(String info) {
        info = username + "：" + info;
        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    读取从服务端回复的消息
    public void readInfo() {
        int readChannels = 0;
        try {
            readChannels = selector.select();
            if (readChannels > 0) {
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey key = iterator.next();
                    if (key.isReadable()) {
//                    道道相关的通道
                        SocketChannel sc = (SocketChannel) key.channel();
//                   得到一个buffer
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
//                        读取
                        sc.read(buffer);
//                        把读到的缓冲区的数据转换成字符串
                        String msg = new String(buffer.array());
                        System.out.println(msg.trim());
                    }
                }
//                删除当前的selectionKey，防止重复操作
                iterator.remove();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
//        启动客户端
        GroupChatClient chatClient = new GroupChatClient();
//        启动一个线程
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    chatClient.readInfo();
                    try {
                        Thread.currentThread().sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

//        发送数据给服务端
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            chatClient.sendInfo(s);
        }
    }
}
